package com.zjy.controller;

import com.zjy.bean.ShoppingCart;
import com.zjy.dto.ApiResult;
import com.zjy.dto.ListDTO;
import com.zjy.service.ShoppingCartService;
import com.zjy.utils.ObjectUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import static com.zjy.api.Api.CLIENT_ERROR;
import static com.zjy.api.Api.SERVER_ERROR;

/**
 * Created by Administrator on 2019/5/19.
 */
@Controller
@CrossOrigin
@RequestMapping("/shopCart")
public class shopCartController {
    @Resource
    private ShoppingCartService shoppingCartService;

    /**
     * 加入购物车
     * @param shoppingCart
     * @return
     */
    @RequestMapping(value = "/addShopCart", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult addShopCart(@RequestBody ShoppingCart shoppingCart){
        ApiResult result = new ApiResult();
        if(ObjectUtils.isNullOrEmpty(shoppingCart.getGoodsId()) || ObjectUtils.isNullOrEmpty(shoppingCart.getUserId())){
            return result.error(CLIENT_ERROR,"userId和goodsId不能为空！");
        }else {
            try{
                shoppingCartService.addShopCart(shoppingCart);
                result.setMsg("加入购物车成功！");
            }catch (Exception e){
                return result.error(SERVER_ERROR,"加入购物车失败，原因："+ e.getMessage());
            }
        }
        return result;
    }

    /**
     * 减去购物车
     * @param shoppingCart
     * @return
     */
    @RequestMapping(value = "/subtractShopCartById", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult subtractShopCart(@RequestBody ShoppingCart shoppingCart){
        ApiResult result = new ApiResult();
        if(ObjectUtils.isNullOrEmpty(shoppingCart.getGoodsId()) || ObjectUtils.isNullOrEmpty(shoppingCart.getUserId())){
            return result.error(CLIENT_ERROR,"userId和goodsId不能为空！");
        }else {
            try{
                int count = shoppingCartService.subtractShopCartById(shoppingCart);
                if(count == 1){
                    result.setMsg("减少购物车成功！");
                }else if(count == 0){
                    result.error(CLIENT_ERROR,"减少购物车失败，找不到此id对应的购物车信息");
                }
            }catch (Exception e){
                return result.error(SERVER_ERROR,"减少购物车失败，原因："+ e.getMessage());
            }
        }
        return result;
    }

    /**
     * 根据购物车id数组批量清空购物车
     * @param listDTO
     * @return
     */
    @RequestMapping(value = "/deleteShopCartById", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult deleteShopCartById(@RequestBody ListDTO listDTO){
        ApiResult result = new ApiResult();
        if(ObjectUtils.isNullOrEmpty(listDTO.getIdList())){
            return result.error(CLIENT_ERROR,"idList不能为空！");
        }else {
            try{
                int count = shoppingCartService.deleteShopCartById(listDTO.getIdList());
                if(count > 0){
                    result.setMsg("删除购物车成功！");
                }else if(count == 0){
                    result.error(CLIENT_ERROR,"删除购物车失败，找不到此id对应的购物车信息");
                }
            }catch (Exception e){
                return result.error(SERVER_ERROR,"删除购物车失败，原因："+ e.getMessage());
            }
        }
        return result;
    }

    /**
     * 查询购物车列表
     * @param shoppingCart
     * @return
     */
    @RequestMapping(value = "/selectShopCartByUserId", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult selectShopCartByUserId(@RequestBody ShoppingCart shoppingCart){
        ApiResult result = new ApiResult();
        if(ObjectUtils.isNullOrEmpty(shoppingCart.getUserId())){
            return result.error(CLIENT_ERROR,"userId不能为空！");
        }else {
            try{
                List<ShoppingCart> shoppingCartList = shoppingCartService.selectShopCartByUserId(shoppingCart.getUserId());
                if(shoppingCartList != null){
                    result.setMsg("查询购物车列表成功！");
                    result.setData(shoppingCartList);
                }else{
                    result.setMsg("此用户购物车列表为空！");
                }
            }catch (Exception e){
                return result.error(SERVER_ERROR,"查询购物车列表失败，原因："+ e.getMessage());
            }
        }
        return result;
    }
}
